package com.dongdong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdong.data.DataResult;
import com.dongdong.data.ResponseStatusEnum;
import com.dongdong.pojo.TbContentCategory;
import com.dongdong.service.ContentCategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Deacription TODO
 * @Author Lenovo
 * @Date 2020/12/16 11:59
 * @Version 1.0
 **/
@Controller
@RequestMapping("contentCategoryController")
public class ContentCategoryController {

    private static final Logger logger = LoggerFactory.getLogger(ContentCategoryController.class);

    @Reference
    private ContentCategoryService contentCategoryService;

    @RequestMapping("list")
    public String list(){
        return "admin/content_category";
    }

    @RequestMapping("getContentCategory")
    @ResponseBody
    public DataResult getContentCategory(String name){
        try {
            List<TbContentCategory> list = contentCategoryService.getContentCategory(name);
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(list);
        }catch (Exception e){
            logger.error("查询错误");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("saveContentCategory")
    @ResponseBody
    public DataResult saveContentCategory(TbContentCategory contentCategory){
        try {
            contentCategoryService.saveContentCategory(contentCategory);
            return DataResult.response(ResponseStatusEnum.SUCCESS);
        }catch (Exception e){
            logger.error("增加失败");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("toUpdate/{id}")
    @ResponseBody
    public DataResult toUpdate(@PathVariable("id") Long id){
        try {
            TbContentCategory tbContentCategory = contentCategoryService.toUpdate(id);
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(tbContentCategory);
        }catch (Exception e){
            logger.error("回显失败");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("updateContentCategory")
    @ResponseBody
    public DataResult updateContentCategory(TbContentCategory contentCategory){
        try {
            contentCategoryService.updateContentCategory(contentCategory);
            return DataResult.response(ResponseStatusEnum.SUCCESS);
        }catch (Exception e){
            logger.error("修改失败");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("deleteBatch")
    @ResponseBody
    public DataResult deleteBatch(@RequestParam("ids[]") Long[] ids){
        try {
            contentCategoryService.deleteBatch(ids);
            return DataResult.response(ResponseStatusEnum.SUCCESS);
        }catch (Exception e){
            logger.error("删除失败");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }
}

